﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Forest.Cat.Admin.Models.Homes;
using Forest.Cat.Domain;
using Forest.Cat.Domain.Services;
using Forest.Cat.Domain.Entities;
using Forest.Cat.Domain.Configuration;
using Forest.Cat.Domain.Services.Applications;
using Forest.Cat.Domain.Services.Catalog;
using Forest.Cat.Domain.Services.Compnaies;

namespace Forest.Cat.Admin.Controllers
{
    public class HomeController : Controller
    {
        private readonly IWorkContext _workContext;
        private readonly IResumeService _resumeService;
        private readonly ILovService _lovService;
        private readonly IApplicationService _applicationService;
        private readonly IJobPositionService _jobPositionService;
        private readonly ICompanyService _companyService;

        public HomeController(IWorkContext workContext, IResumeService resumeService, ILovService lovService,
            IApplicationService applicationService, IJobPositionService jobPositionService, ICompanyService companyService)
        {
            _workContext = workContext;
            _resumeService = resumeService;
            _lovService = lovService;
            _applicationService = applicationService;
            _jobPositionService = jobPositionService;
            _companyService = companyService;
        }

        public ActionResult Index()
        {
            //已申请过的职位列表
            var appliedJobIdList = new List<Guid>();
            //if (_workContext.CurrentUser != null)
            //{
            //    appliedJobIdList = _applicationService.GetApplicationsByJobseekerId(
            //        _workContext.CurrentUser.Id).Select(x => x.JobPositionId).ToList();
            //}
            var list = _jobPositionService.GetAdminIndexList(_workContext.CurrentUser.Id);
            var item = new IndexListModel()
            {
                IndexList = list.Take(10).ToList().Select(s => PrepareJobModel(s, appliedJobIdList)).ToList()
            };

            return View(item);
        }

        public ActionResult About()
        {
            return View();
        }

        public PartialViewResult PersonInfo()
        {
            var companyLetterCount = _jobPositionService.GetReceivedMessageList(_workContext.CurrentUser.Id).Count();
            var item = new PersonInfoModel()
            {
                Name = _workContext.CurrentUser.FullName,
                Id = _workContext.CurrentUser.Id,
                Email = _workContext.CurrentUser.Email,
                LastLogin = _workContext.CurrentUser.LastSignedIn,
                CompanyLetterCount = companyLetterCount
            };

            var resumeList = _resumeService.GetResumes(item.Id);
            item.Views = resumeList.Sum(s => s.Views ?? 0);
            var lastItem = resumeList.OrderByDescending(o => o.LastUpdated).FirstOrDefault();
            if (lastItem != null)
            {
                item.LastUpdate = lastItem.LastUpdated;

            }
            return PartialView(item);
        }

        #region PrepareJobModel
        /// <summary>
        /// PrepareJobModel
        /// </summary>
        /// <param name="job"></param>
        /// <param name="appliedJobIdList"></param>
        /// <returns></returns>
        [NonAction]
        private IndexModel PrepareJobModel(JobPosition job, List<Guid> appliedJobIdList)
        {
            var jobModel = job.ToModel();
            jobModel.Responsibilities = job.Responsibilities ?? string.Empty;
            jobModel.YearsOfExperienceName = _lovService.GetLovName(job.YearsOfExperience.Value);
            jobModel.EducationLevelName = _lovService.GetLovName(job.EducationLevel.Value);
            jobModel.JobTypeName = _lovService.GetLovName(job.JobType);
            jobModel.CityName = _lovService.GetLovName(job.City);
            //jobModel.DistrictName = job.District.HasValue ? _lovService.GetLovName(job.District.Value) : String.Empty;
            jobModel.CompanyName = string.Empty;
            if (job.CompanyId.HasValue)
            {
                var company = _companyService.GetCompanyById(job.CompanyId.Value);
                if (company != null) 
                {
                    jobModel.CompanyName = company.CompanyName;
                    jobModel.CompanyId = job.CompanyId.Value;
                }
            }
            if (appliedJobIdList != null)
            {
                jobModel.IsApplied = appliedJobIdList.Any(x => x.Equals(job.Id));
            }
            return jobModel;
        }
        #endregion
    }
}
